/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DTO;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author ThanhTai
 */
public class ProjectsUpdateSAXParser extends DefaultHandler {

    private Project project = null;
    private Date startDay = null;
    private Date endDay = null;
    private boolean bProjectID = false;
    private boolean bDescription = false;
    private boolean bStatus = false;
    private boolean bStart = false;
    private boolean bSDyear = false;
    private boolean bSDmonth = false;
    private boolean bSDday = false;
    private boolean bEnd = false;
    private boolean bSEyear = false;
    private boolean bSEmonth = false;
    private boolean bSEday = false;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equals("project")) {
            project = new Project();
        }
        if (qName.equals("projectID")) {
            bProjectID = true;
        }
        if (qName.equals("description")) {
            bDescription = true;
        }
        if (qName.equals("status")) {
            bStatus = true;
        }
        if (qName.equals("startDay")) {
            startDay = new Date();
            bStart = true;
        }
        if (qName.equals("year") && bStart) {
            bSDyear = true;
        }
        if (qName.equals("month") && bStart) {
            bSDmonth = true;
        }
        if (qName.equals("day") && bStart) {
            bSDday = true;
        }
        if (qName.equals("endDay")) {
            endDay = new Date();
            bEnd = true;
        }
        if (qName.equals("year") && bEnd) {
            bSEyear = true;
        }
        if (qName.equals("month") && bEnd) {
            bSEmonth = true;
        }
        if (qName.equals("day") && bEnd) {
            bSEday = true;
            bEnd = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String str = new String(ch, start, length);
        if (bProjectID) {
            project.setProjectID(Integer.parseInt(str));
            bProjectID = false;
        }
        if (bDescription) {
            project.setDescription(str);
            bDescription = false;
        }
        if (bStatus) {
            project.setStatus(str);
            bStatus = false;
        }
        if (bSDyear && bStart) {
            startDay.setYear(Integer.parseInt(str));
            bSDyear = false;
        }
        if (bSDmonth && bStart) {
            startDay.setMonth(Integer.parseInt(str));
            bSDmonth = false;
        }
        if (bSDday && bStart) {
            startDay.setDay(Integer.parseInt(str));
            bSDday = false;
            bStart = false;
            project.setStartDay(startDay);
        }
        if (bSEyear && bEnd) {
            endDay.setYear(Integer.parseInt(str));
            bSEyear = false;
        }
        if (bSEmonth && bEnd) {
            endDay.setMonth(Integer.parseInt(str));
            bSEmonth = false;
        }
        if (bSEday && bEnd) {
            endDay.setDay(Integer.parseInt(str));
            bSEday = false;
            bStart = false;
            project.setEndDay(endDay);
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equals("project")) {
            project.UpdateProject();
            project = null;
        }
    }
}
